##################################################################
# File : /lua/common/commonutils.lua
# Description : Common utils
# Author : GPG
##################################################################

function GetMassStorage(unit)
    if IsSim() then
        return SimGetEconomyTotals(unit)["stored"]["MASS"] or 0
    else
        return GetEconomyTotals()["stored"]["MASS"] or 0
    end
end

function ResolveUnit(id)
    if IsSim() then
        local thing = GetEntityById(id)
        if thing then
            local blip = IsBlip(thing)
            if blip then
                return blip:GetSource()
            end
            
            local unit = IsUnit(thing)
            if unit then
                return unit
            end

            error('Entity id ' .. id .. ' is not a unit or blip.')
        end
        return thing
    else
        return GetUnitById(id)
    end
end

function GetSyncData(entity)
    if IsSim() then
        return entity.Sync
    else
        # If the entity's id is not found in the EntityData table, see if it's a blip,
        # resolve the id, and return any syncdata we might have on the blip.
        local id = entity:GetEntityId()
        local data = EntityData[id]
        if not data then
            id = BlipMap[id]
            if id then
                data = EntityData[id]
            end
        end
        return data
    end
end

function GetSyncDataById(id)
    if IsSim() then
        local ent = GetEntityById(id)
        if ent then
            return ent.Sync
        end
    else
        # If the entity's id is not found in the EntityData table, see if it's a blip,
        # resolve the id, and return any syncdata we might have on the blip.
        local data = EntityData[id]
        if not data then
            id = BlipMap[id]
            if id then
                data = EntityData[id]
            end
        end
        return data
    end        
end

function GetInventory(entity, inventoryName)
    if entity then
        if IsSim() then
            return entity.Sync.Inventory[inventoryName]
        else
            local syncData = EntityData[entity:GetEntityId()]
            if not syncData then
                local id = BlipMap[entity:GetEntityId()]
                if id then
                    syncData = EntityData[id]
                end
            end
            if syncData then
                return syncData.Inventory[inventoryName]
            end
        end
    end
end

function GetAllInventories(entity)
    if entity then
        if IsSim() then
            return entity.Sync.Inventory
        else
            local syncData = EntityData[entity:GetEntityId()]
            if not syncData then
                local id = BlipMap[entity:GetEntityId()]
                if id then
                    syncData = EntityData[id]
                end
            end
            if syncData then
                return syncData.Inventory
            end
        end
    end
end

function GetShopTree(unit)
    local shopTree = GetBlueprint(unit).Shop.Tree
    if not shopTree then
        error("Shop tree undefined for unit: " .. GetBlueprint(unit).BlueprintId)
    end
    return shopTree
end

function GetShopTreeByBlueprint(shopBp)
    local shopTree = shopBp.Shop.Tree
    if not shopTree then
        #error("Shop tree undefined for unit: " .. shopBp.BlueprintId)
    end
    return shopTree
end
